<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderCode">
        <el-input v-model="queryParams.orderCode" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="审核状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable class="select-width">
          <el-option
            v-for="dict in dict.type.evaluation_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['operator:evaluation:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['operator:evaluation:edit']"-->
      <!--        >审核</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operator:evaluation:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['operator:evaluation:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="resetQuery"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="evaluationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单编号" align="center" prop="orderCode"/>
      <el-table-column label="是否满意" align="center" prop="satisfied"/>
      <el-table-column label="评价" align="center" prop="evaluation"/>
      <el-table-column label="图片" align="center" prop="pic">
        <template slot-scope="scope">
          <div v-for="(item,index) in scope.row.picList" v-if="scope.row.picList.length > 0" style="margin-left: 5px;width: 50px;height: 50px;display: inline-block"
               @click="showPic(item)">
            <img :src="item" style="width: 100%;height: 100%">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="站点" align="center" prop="stationName"/>
      <!--<el-table-column label="满意原因" align="center" prop="satisfiedReason"/>-->
      <el-table-column label="审核状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.evaluation_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['operator:evaluation:edit']"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['operator:evaluation:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或审核用户订单评价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户名称" prop="customName">
          <el-input v-model="form.customName" placeholder="客户名称" readonly/>
        </el-form-item>
        <el-form-item label="openid" prop="customCode">
          <el-input v-model="form.customCode" placeholder="openid" readonly/>
        </el-form-item>
        <el-form-item label="订单编号" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入订单编号" readonly/>
        </el-form-item>
        <el-form-item label="是否满意" prop="satisfied">
          <el-input v-model="form.satisfied" placeholder="请输入是否满意" readonly/>
        </el-form-item>
        <el-form-item label="评价" prop="evaluation">
          <el-input v-model="form.evaluation" placeholder="请输入评价" readonly/>
        </el-form-item>
        <el-form-item label="图片" prop="pic"><!--display: inline-->
          <div v-for="(item,index) in form.picList" v-if="form.picList.length > 0" style="margin-left: 5px;width: 50px;height: 50px;display: inline-block"
               @click="showPic(item)">
            <img :src="item" style="width: 100%;height: 100%">
          </div>
        </el-form-item>
        <el-form-item label="站点" prop="stationName">
          <el-input v-model="form.stationName" placeholder="请输入站点" readonly/>
        </el-form-item>
        <el-form-item label="满意原因" prop="satisfiedReason" v-if="form.satisfied == '满意'">
          <el-input v-model="form.satisfiedReason" type="textarea" placeholder="请输入满意原因" readonly/>
        </el-form-item>
        <el-form-item label="审核状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.evaluation_state"
              :key="dict.value" :label="dict.value"
            >{{dict.label}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="拒绝原因" prop="refuseReason" v-if="form.state == '2'">
          <el-input v-model="form.refuseReason" type="textarea" placeholder="请输入拒绝原因"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible" title="预览" width="800" append-to-body>
      <img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto; height: 750px"/>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listEvaluation,
    getEvaluation,
    delEvaluation,
    addEvaluation,
    updateEvaluation
  } from '@/api/smart/custom/evaluation'

  export default {
    name: 'Evaluation',
    // 字典
    dicts: [
      'evaluation_state'
    ],
    data() {
      return {
        dialogVisible:false,
        dialogImageUrl:'',
        //上传图片路径
        reqFileUrl: 'https://www.sdzctech.cn',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        orderCodes: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户订单评价表格数据
        evaluationList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          // customCode: null,
          // orderCode: null,
          // satisfied: null,
          // evaluation: null,
          // pic: null,
          // stationId: null,
          // satisfiedReason: null,
          state: null,
          orderCode: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          state: [
            { required: true, message: '请选择审核状态', trigger: 'change' }
          ],
          refuseReason: [
            { required: true, message: '拒绝原因不能为空', trigger: 'blur' }
          ]
        },
        stateList: [{ value: '0', label: '否' }, { value: '1', label: '是' }]
      }
    },
    created() {
      this.getList()
    },
    methods: {
      showPic(item){
        this.dialogImageUrl = item
        this.dialogVisible = true
      },
      /** 查询用户订单评价列表 */
      getList() {
        this.loading = true
        listEvaluation(this.queryParams).then(response => {
          this.evaluationList = response.rows
          this.evaluationList.forEach(item=>{
            console.log(item.picList.length)
          })
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          customCode: null,
          orderCode: null,
          satisfied: null,
          evaluation: null,
          pic: null,
          createTime: null,
          stationId: null,
          satisfiedReason: null,
          state: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.orderCodes = selection.map(item => item.orderCode)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加用户订单评价'
      },
      /** 审核按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getEvaluation(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '审核用户订单评价'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateEvaluation(this.form).then(response => {
                this.$modal.msgSuccess('审核成功')
                this.open = false
                this.getList()
              })
            } else {
              addEvaluation(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        const orderCodes = row.orderCode || this.orderCodes
        this.$modal.confirm('是否确认删除订单号为"' + orderCodes + '"的数据项？').then(function() {
          return delEvaluation(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('operator/evaluation/export', {
          ...this.queryParams
        }, `evaluation_${new Date().getTime()}.xlsx`)
      }
    }
  }
</script>
